package com.campol.activity;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;

import com.campol.MainActivity;
import com.campol.MainActivity.OnPostListChangedListener;
import com.campol.MainActivity.OnReporterChangedListener;
import com.campol.R;
import com.campol.adapter.PostsAdapter;
import com.campol.adapter.ReportersAdapter;
import com.campol.model.PostModel;
import com.campol.model.Reporter;
import com.campol.utils.Constants;

public class PostsListingActivity extends Activity implements OnClickListener, OnPostListChangedListener, OnItemClickListener,
															  OnReporterChangedListener {

	public final static int ITEM_TYPE_POST = 1;
	public final static int ITEM_TYPE_REPORTER = 2;
	
	private ListView lvPosts;
	private Button btnPosts, btnTopReps;
	
	private boolean topReportersPressed;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		init();
	}

	private void init() {
		setContentView(R.layout.post_listing_screen);
		
		lvPosts = (ListView) findViewById(R.id.posts_listview);
		PostsAdapter adapter = new PostsAdapter(this, new ArrayList<PostModel>());
		lvPosts.setAdapter(adapter);
		lvPosts.setOnItemClickListener(this);
		
		btnPosts = (Button) findViewById(R.id.posts_posts_listing);
		btnTopReps = (Button) findViewById(R.id.posts_reporters_listing);
		
		btnPosts.setOnClickListener(this);
		btnTopReps.setOnClickListener(this);
		btnPosts.setBackgroundResource(R.drawable.left_section_btn_pressed);
		
		((MainActivity)getParent()).addObserver(this);
		((MainActivity)getParent()).setOnReportersListener(this);
	}

	@Override
	protected void onStart() {
		super.onStart();
		ArrayList<PostModel> items = ((MainActivity)getParent()).getPosts();
		if (items!=null) {
			onPostListChanged(items);
		} else {
			String type = PreferenceManager.getDefaultSharedPreferences(this).getString(SettingsActivity.SEARCH_AREA_KEY, Constants.GET_TYPE_NEARBY);
			((MainActivity)getParent()).makeRequestForPostsListWithType(type);
		}
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.posts_posts_listing:
			if (!topReportersPressed) break;
			topReportersPressed = false;
			btnPosts.setBackgroundResource(R.drawable.left_section_btn_pressed);
			btnTopReps.setBackgroundResource(R.drawable.right_section_btn);
			
			ArrayList<PostModel> items = ((MainActivity)getParent()).getPosts();
			if (items != null) {
				lvPosts.setAdapter(new PostsAdapter(this, items));
			} else {
				String type = PreferenceManager.getDefaultSharedPreferences(this).getString(SettingsActivity.SEARCH_AREA_KEY, Constants.GET_TYPE_NEARBY);
				((MainActivity)getParent()).makeRequestForPostsListWithType(type);
			}
			break;
		case R.id.posts_reporters_listing:
			if (topReportersPressed) break;
			topReportersPressed = true;
			btnPosts.setBackgroundResource(R.drawable.left_section_btn);
			btnTopReps.setBackgroundResource(R.drawable.right_section_btn_pressed);
			
			ArrayList<Reporter> itms = ((MainActivity)getParent()).getReporters();
			if (itms != null) {
				lvPosts.setAdapter(new ReportersAdapter(this, itms));
			} else {
				((MainActivity)getParent()).makeRequestForTopReporters();
			}
			break;
		}
	}

	@Override
	public void onPostListChanged(ArrayList<PostModel> items) {
		if (!topReportersPressed) {
			lvPosts.setAdapter(new PostsAdapter(this, items==null ? new ArrayList<PostModel>():items));
		}
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		if (lvPosts.getAdapter().getItemViewType(arg2) == ITEM_TYPE_REPORTER) return;
		PostModel item = ((PostsAdapter)lvPosts.getAdapter()).getItem(arg2);
		((MainActivity)getParent()).setTempPost(item);
		((MainActivity)getParent()).switchToMapTab();
	}

	@Override
	public void onReporterChanged(ArrayList<Reporter> items) {
		if (topReportersPressed) {
			lvPosts.setAdapter(new ReportersAdapter(this, items==null ? new ArrayList<Reporter>() : items));
		}
	}
	
}
